package com.wzc.basic.controller;


import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.service.IVerifyCodeService;
import com.wzc.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{key}")
    public AjaxResult getVerifyCode(@PathVariable("key") String key){

        try {
            String baseStr = verifyCodeService.getVerifyCode(key);

            // 将数据封装返回给页面
            return AjaxResult.me().setResultObj(baseStr);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("获取验证码失败！请稍后重试！");
        }
    }


    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody Map<String, String> map){
        try {
            verifyCodeService.smsCode(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("获取验证码失败！请稍后重试！");
        }
    }

    /**
     * 绑定时用到的验证码
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody Map<String, String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("获取验证码失败！请稍后重试！");
        }
    }






}
